package com.finstone.data.spider.eshop.processor;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.Html;
import us.codecraft.webmagic.selector.Selectable;

@Component
public class EShopGoodsDetailParamProcessor implements PageProcessor {

	private final Logger logger = LoggerFactory.getLogger(getClass());

	private Site site = Site.me().setCycleRetryTimes(3).setTimeOut(30000);

	public Site getSite() {
		site.addHeader("Referer", "http://www.lenovo.com.cn/");
		site.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.101 Safari/537.36");
		return site;
	}

	public void parseShopVariable(Page page) {

		Map<String, Map<String, Object>> variableMap = new LinkedHashMap<String, Map<String, Object>>();
		String key_str = "";
		String val_items_str = "";
		Html html = page.getHtml();
		for (Selectable tr : html.$(".ns_bd > table > tbody > tr").nodes()) {
			Map<String, Object> variableMap_item = new LinkedHashMap<String, Object>();
			String key_main = "";
			if(tr.$("th[colspan=2]").match()){
				key_main = tr.$("th[colspan=2]","text").toString();
				for(Selectable temptr : html.$(".ns_bd > table > tbody > tr").nodes()) {
					if( temptr.$("th[colspan=2]").match()) {
						String tempstr = temptr.$("th[colspan=2]", "text").toString();
						if(key_str.contains(tempstr)){
							continue;
						}else{
							if (!tempstr.equals(key_main)) {
								break;
							}
						}
					}else{
						if (temptr.$("td").match()) {
							if(val_items_str.contains(temptr.$("th", "text").toString())){
								continue;
							}
							String key_item = temptr.$("th", "text").toString();
							String val_item = temptr.$("td", "text").toString();
							val_items_str += key_item + "|";
							variableMap_item.put(StringUtils.trim(StringUtils.replace(key_item, "\"", "")), StringUtils.trim(StringUtils.replace(val_item, "\"", "")));
						}
					}
				}
			}else{
				continue;
			}
			key_str += key_main + "|";
			variableMap.put(key_main, variableMap_item);
		}
		page.putField("goods_id", page.getRequest().getExtra("goods_id"));
		page.putField("variables", variableMap);
	}

	public void process(Page page) {
		parseShopVariable(page);
	}
}